package tree.binary;

import entity.tree.TreeNode;

public class leetcode671二叉树中第二小的节点 {
    double secMin = Double.MAX_VALUE;
    public int findSecondMinimumValue(TreeNode root) {
        int min = root.val;
        dfs(root, min);
        return secMin == Double.MAX_VALUE ? -1 : (int) secMin;
    }

    public void dfs(TreeNode root, int min) {
        if (root == null) {
            return;
        }
        if (root.val != min) {
            secMin = Double.min(secMin, root.val);
        }
        dfs(root.left, min);
        dfs(root.right, min);
    }
}
